package com.talent.core.protocol.mqtt.handler;

import com.talent.core.protocol.mqtt.config.Constant;
import com.talent.core.protocol.mqtt.model.request.TlMqttPubCompReq;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPubCompVariableHead;
import com.talent.core.protocol.mqtt.store.IMqttStore;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：ConnectHandler
 * @Author: hszhou
 * @Date: 2024/11/25 11:06
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@ChannelHandler.Sharable
public class TlMqttPubCompHandler extends SimpleChannelInboundHandler<TlMqttPubCompReq> {

    private static final  Logger logger = LoggerFactory.getLogger(TlMqttPubCompHandler.class);

    private final IMqttStore mqttStore;

    public TlMqttPubCompHandler(IMqttStore mqttStore){
        this.mqttStore = mqttStore;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TlMqttPubCompReq msg) throws Exception {

        logger.debug("进入到【PUBCOMP】的处理器");
        Object clientId = ctx.channel().attr(AttributeKey.valueOf(Constant.CLIENT_ID)).get();
        if(clientId!=null){
            TlMqttPubCompVariableHead variableHead = msg.getVariableHead();
            Integer messageId = variableHead.getMessageId();
            mqttStore.removePubRec(messageId);
            logger.debug("收到客户端{}的消息{}确定",clientId,variableHead.getMessageId());
        }

    }
}
